package com.wn.system.controller;

import com.wn.common.model.bank.dto.CardBindDto;
import com.wn.common.model.bank.dto.SignatureDto;
import com.wn.common.model.user.dto.LoginDto;
import com.wn.common.model.user.dto.UserRegisterDto;
import com.wn.common.model.user.vo.LoginVo;
import com.wn.common.model.user.vo.UserInfoVo;
import com.wn.common.result.Result;
import com.wn.system.entity.User;
import com.wn.system.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2024-09-03
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private IUserService userService;

    @GetMapping("/list")
    public List<User> list(){
        return  userService.list();
    }

    /**
     *  @Valid  开启数据校验
     *  IOC 的 理解
      * @param user
     * @return
     */
    @PostMapping("register")
    public Result register(@RequestBody @Valid UserRegisterDto user){

        userService.register(user);

        return Result.ok();
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = userService.login(loginDto);
        return Result.ok(loginVo);
    }

    /**
     * 用户绑卡
     */
    @PostMapping("/bank/card/add")
    public Result<SignatureDto> addBankAccount(@RequestBody CardBindDto cardBindDto) {
        //绑卡
        SignatureDto signatureDto = userService.addBankAccount(cardBindDto);
        return  Result.ok(signatureDto);
    }

    /**
     * 根据用户id查询
     */
    @ApiOperation("根据用户id查询")
    @GetMapping("/inner/findById/{userId}")
    public Result<UserInfoVo> findById(@PathVariable("userId") Long userId) {
        UserInfoVo userInfoVo = userService.findById(userId);
        return Result.ok(userInfoVo);
    }
}
